from PyQt5.QtWidgets import QDialog, QVBoxLayout, QHBoxLayout, QFrame, QLabel, QPushButton, QTextEdit, QMessageBox
from config import frame_config,Text_config,input_config,button_config


class Comment(QDialog):
    def __init__(self, parent=None):
        super(Comment, self).__init__(parent)
        self.parent = parent
        self.client=self.parent.client
        self.order_id=self.parent.order_id
        self.shop_name=self.parent.shop_name
        title=f'对{self.shop_name}的评论'
        self.resize(600,500)
        self.setWindowTitle(title)
        mainLayout = QVBoxLayout(self)
        self.initUI(mainLayout)
    def initUI(self,mainLayout):
        frame=QFrame()
        mainLayout.addWidget(frame)
        layout=QHBoxLayout(frame)
        frame.setStyleSheet(frame_config.frame_style3)
        label=QLabel()
        label.setText('你给我们的服务打几分')
        label.setStyleSheet(Text_config.text_style2)
        layout.addWidget(label)

        btn_sub = QPushButton()
        btn_sub.setText('-')
        btn_sub.setFixedSize(40, 50)
        self.num = QTextEdit()
        self.num.setReadOnly(True)
        self.num.setText('1')
        self.num.setStyleSheet(input_config.input_style6)
        self.num.setFixedSize(50, 50)
        btn_add = QPushButton()
        btn_add.setText('+')
        btn_add.setFixedSize(40, 50)
        layout.addWidget(btn_sub)
        layout.addWidget(self.num)
        layout.addWidget(btn_add)

        self.text = QTextEdit()
        self.text.setReadOnly(True)
        self.text.setText('你还未作出评论')
        self.text.setStyleSheet(input_config.input_style6)
        self.text.setFixedSize(200, 50)
        layout.addWidget(self.text)


        btn_sub.clicked.connect(self.sub_num_clicked)
        btn_add.clicked.connect(self.add_num_clicked)
        self.num.textChanged.connect(self.text_changed)


        frame=QFrame()
        mainLayout.addWidget(frame)
        frame.setStyleSheet(frame_config.frame_style1)
        layout=QVBoxLayout(frame)
        self.comment=QTextEdit()
        self.comment.setPlaceholderText('请输入对我们的建议')
        self.comment.setFixedHeight(200)
        layout.addWidget(self.comment)

        layout1=QHBoxLayout()
        layout.addLayout(layout1)
        btn_submit = QPushButton()
        btn_submit.setText('提交')
        btn_submit.setFixedHeight(50)
        btn_submit.setStyleSheet(button_config.button_style2)
        layout1.addWidget(btn_submit)

        layout1=QHBoxLayout()
        layout.addLayout(layout1)
        exit_btn = QPushButton()
        exit_btn.setText('返回')
        exit_btn.setFixedHeight(50)
        exit_btn.setStyleSheet(button_config.button_style4)
        layout1.addWidget(exit_btn)

        btn_submit.clicked.connect(self.submit)
        exit_btn.clicked.connect(self.exit)

    #减少数量
    def sub_num_clicked(self):
        num=int(self.num.toPlainText())-1
        if num>=0:
            self.num.setText(str(num))

    # 增加数量
    def add_num_clicked(self):
        num = int(self.num.toPlainText()) + 1
        if num<=5:
            self.num.setText(str(num))

    def text_changed(self):
        text=int(self.num.toPlainText())
        if text == 0:
            self.text.setText('你还未作出评论')
        elif text == 1:
            self.text.setText('🤬🤬答辩玩意')
        elif text == 2:
            self.text.setText('😠😠不愿多说')
        elif text == 3:
            self.text.setText('😑😑马马虎虎')
        elif text == 4:
            self.text.setText('😗😗还不错哟')
        else:
            self.text.setText('😍😍十分喜欢')

    def submit(self):
        star=int(self.num.toPlainText())
        comment=self.comment.toPlainText()
        if star==0 or comment=='':
            QMessageBox.critical(None,'错误','请填写完整')
            return
        else:
            res=QMessageBox.question(None,'提示','确定提交吗',QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            if res == QMessageBox.Yes:
                parser={
                    'order_id':self.order_id,
                    'star':star,
                    'content':comment,
                }
                self.client.set_comment(parser)
                self.exit()
            else:
                return


    def exit(self):
        self.close()
        self.accept()

    def accept(self):
        super().accept()



